package com.atguigu.cmsservice.controller;


import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 * 后台
 * @author shaki
 * @since 2021-06-20
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {
    @Resource
    private CrmBannerService bannerService;
    /**
     * 分页查询
     * @return
     */
    @GetMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable("page") long page, @PathVariable("limit") long limit){
        Page<CrmBanner> bannerPage = new Page<>(page,limit);
        bannerService.page(bannerPage,null);
        return R.ok().data("items",bannerPage.getRecords()).data("total",bannerPage.getTotal());
    }

    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner banner){
        bannerService.save(banner);
        return R.ok();
    }

    @PostMapping("/updateBanner")
    public R updateBannerById(@RequestBody CrmBanner banner){
        bannerService.updateById(banner);
        return R.ok();
    }

    @DeleteMapping("/removeBanner/{id}")
    public R removeBanner(@PathVariable("id") String id){
        bannerService.removeById(id);
        return R.ok();
    }

    /**
     * 根据id查询
     */
    @GetMapping("getBannerInfoById/{id}")
    public R getBannerInfoById(@PathVariable("id") String id){
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item",banner);
    }
}

